package com.alibaba.nacos.naming.myinterface;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.naming.boot.RunningConfig;
import com.alibaba.nacos.naming.mapper.heartbeat.HeartbeatMapper;
import com.alibaba.nacos.naming.pojo.HeartbeatServicePojo;
import com.alibaba.nacos.naming.utils.HttpUtil;
import com.alibaba.nacos.naming.utils.KafkaUtil;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Configuration
public class MyInterface {

    private static final Logger log = LoggerFactory.getLogger(MyInterface.class);

    //调用token
    private static final String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJkMjB.ivr";

    @Autowired
    private HttpUtil httpUtil;

    @Autowired
    private HeartbeatMapper heartbeatMapper;

    @Autowired
    private KafkaUtil kafkaUtil;

    @Value("${nacos.serviceEnv}")
    private String serviceEnv;

    @Async
    public void sendWhile(String serviceName, Instance instance, int type, int count) throws InterruptedException {
        while (count-- > 0){
            sendMsg(serviceName, instance, type);
            if(count > 0) {
                Thread.sleep(1000 * 60 * 30);
            }
        }
    }

    /**
     * 发送短信
     */
    public void sendMsg(String serviceName, Instance instance, int type){
        try{
            String URL = "http://127.0.0.1:"+RunningConfig.getServerPort()+"/nacos/sms/batchSendWarnMessage";

            Map<String, String> keyMap = new HashMap<>();
            keyMap.put("x-auth-token", token);

            String answerContent = "";
            if(StringUtil.isNullOrEmpty(serviceName)) return;
            String[] arr = serviceName.split("@@");
            HeartbeatServicePojo pojo = heartbeatMapper.getHeartbeatServiceConfig(arr[0], arr[1]);

            if(pojo != null){
                serviceName = String.format("[%s][%s][%s]",pojo.getServiceSystem(),pojo.getServiceDescribe(),pojo.getServiceName());
            }

            //主动下线
            if(type == 0){
                answerContent = serviceName + "(" + instance.getIp() + ":" + instance.getPort() + ")" + " 主动下线";
            }else {//故障下线
                answerContent = serviceName + "(" + instance.getIp() + ":" + instance.getPort() + ")" + " 故障下线";
            }

            answerContent = "[" + serviceEnv + "环境]" + answerContent;

            String param = "{\"context\":\""+answerContent+"\"}";
            System.out.println("sendmsg--->param: " + param);
            Map<String, String> rspMap = httpUtil.getURLContentPostaddProperty(URL, param, "application/json", 3000, "UTF-8", keyMap);

            String code = rspMap.get("code");
            String msg = rspMap.get("msg");
            System.out.println("sendmsg--->code: " + code + ",msg: " + msg);
        }catch (Exception ex){
            log.error("Exception: ", ex);
        }
    }

    /**
     * 发送短信
     */
    @Async
    public void sendMsgOnline(String serviceName, Instance instance){
        try{
            String URL = "http://127.0.0.1:"+RunningConfig.getServerPort()+"/nacos/sms/batchSendWarnMessage";

            Map<String, String> keyMap = new HashMap<>();
            keyMap.put("x-auth-token", token);

            String answerContent = "";
            if(StringUtil.isNullOrEmpty(serviceName)) return;
            String[] arr = serviceName.split("@@");
            HeartbeatServicePojo pojo = heartbeatMapper.getHeartbeatServiceConfig(arr[0], arr[1]);

            if(pojo != null){
                serviceName = String.format("[%s][%s][%s]",pojo.getServiceSystem(),pojo.getServiceDescribe(),pojo.getServiceName());
            }

            answerContent = "[" + serviceEnv + "环境]" + serviceName + "(" + instance.getIp() + ":" + instance.getPort() + ")" + " 已经上线";

            String param = "{\"context\":\""+answerContent+"\"}";
            System.out.println("sendMsgOnline--->param: " + param);
            Map<String, String> rspMap = httpUtil.getURLContentPostaddProperty(URL, param, "application/json", 3000, "UTF-8", keyMap);

            String code = rspMap.get("code");
            String msg = rspMap.get("msg");
            System.out.println("sendMsgOnline--->code: " + code + ",msg: " + msg);
        }catch (Exception ex){
            log.error("Exception: ", ex);
        }
    }

}
